package com.dingdayu.club.admin.api;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.dingdayu.club.admin.LoginActivity;
import com.dingdayu.club.admin.MainActivity;
import com.dingdayu.club.admin.common.util.HttpUtils;
import com.dingdayu.club.admin.common.util.SharePreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.ViewInject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dingdayu on 2015-10-22.
 */
public class GetClubInfo {

    public static String CLUB_INFO_FILE = "club_info";
    public static SharePreferenceUtil spU;
    public static HashMap<String,String> hm;

    public GetClubInfo(){
        spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);
    }

    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle data = msg.getData();
            String val = data.getString("value");
            Log.i("GetClubINFO", "请求结果为-->" + val);
            get2(val);
        }
    };

    static Runnable runnable = new Runnable(){
        @Override
        public void run() {
            //
            // TODO: http request.
            //

            int ClubID = spU.getClubID();
            Log.i("GetClubINFO", "ClubID:" + String.valueOf(ClubID));
            KJHttp kjh = new KJHttp();
            HttpParams params2 = new HttpParams();
            params2.put("club_id", String.valueOf(ClubID));

            //HttpCallback中有很多方法，可以根据需求选择实现
            kjh.post("http://corp.xyser.com/Home/Api/getClubInfo", params2, new HttpCallBack() {
                @Override
                public void onSuccess(String t) {
                    Log.i("GetClubINFO", "onSuccess:" + t);
                    get2(t);
                    Message msg = new Message();
                    Bundle data = new Bundle();
                    data.putString("value", t);
                    msg.setData(data);
                    handler.sendMessage(msg);
                    super.onSuccess(t);
                }
            });

        }
    };

    public static HashMap<String,String> getINFO(){
        HashMap<String, String> params = new HashMap<String, String>();
        int ClubID = spU.getClubID();
        Log.e("ClubID", String.valueOf(ClubID));
        params.put("club_id", String.valueOf(ClubID));
        String t = HttpUtils.submitPostData("http://corp.xyser.com/Home/Api/getClubInfo", params, "utf-8");
        Log.e("LoginAPI", t);

        try {
            JSONObject jsonObj = new JSONObject(t);
            int code = jsonObj.getInt("code");
            String msg = jsonObj.getString("msg");
            if (code == 302) {
                // new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定", null).show();
                return hm;
            }

            if (code == 200) {
                JSONObject data = jsonObj.getJSONObject("data");

                hm.put("school",data.getString("school")) ;
                hm.put("name",data.getString("name")) ;
                hm.put("logo_img",data.getString("logo_img")) ;
                hm.put("qrcode_file",data.getString("qrcode_file")) ;
                hm.put("summary",data.getString("summary")) ;
                hm.put("levl",data.getString("levl")) ;
                hm.put("type",data.getString("type")) ;

//                String school = data.getString("school");
//                String name = data.getString("name");
//                String logo_img = data.getString("logo_img");
//                String qrcode_file = data.getString("qrcode_file");
//                String summary = data.getString("summary");
//                String levl = data.getString("levl");
//                String type = data.getString("type");

                saveClubInfo(hm);
                return hm;
            }
        }catch (JSONException e) {
            //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
            Log.e("LoginAPI", "返回数据解析错误" + e.getMessage());
            //login_ret = false;
            //e.printStackTrace();
            return hm;
        }
        return hm;
    }

    public static void get(){
        new Thread(runnable).start();
    }


    public static void get2(String t){


        HashMap<String,String> hm = new HashMap<String,String>() ;

        Log.e("LoginAPI", t);

        if(t == null){
            //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
        }
        try {
            JSONObject jsonObj = new JSONObject(t);
            int code = jsonObj.getInt("code");
            String msg = jsonObj.getString("msg");
            if (code == 302) {
               // new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定", null).show();
            }

            if (code == 200) {
                JSONObject data = jsonObj.getJSONObject("data");

                hm.put("school",data.getString("school")) ;
                hm.put("name",data.getString("name")) ;
                hm.put("logo_img",data.getString("logo_img")) ;
                hm.put("qrcode_file",data.getString("qrcode_file")) ;
                hm.put("summary",data.getString("summary")) ;
                hm.put("levl",data.getString("levl")) ;
                hm.put("type",data.getString("type")) ;

//                String school = data.getString("school");
//                String name = data.getString("name");
//                String logo_img = data.getString("logo_img");
//                String qrcode_file = data.getString("qrcode_file");
//                String summary = data.getString("summary");
//                String levl = data.getString("levl");
//                String type = data.getString("type");

                saveClubInfo(hm);
            }
        }catch (JSONException e) {
            //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
            Log.e("LoginAPI", "返回数据解析错误" + e.getMessage());
            //login_ret = false;
            //e.printStackTrace();
        }
    }

    private static void saveClubInfo(HashMap<String,String> hm){
        Log.e("LoginAPI", "保存登陆数据");
        SharedPreferences preferences = MainActivity.mainActivity.getSharedPreferences(CLUB_INFO_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        // 存入数据
        editor.putString("school", hm.get("school"));
        editor.putString("name", hm.get("name"));
        editor.putString("logo_img", hm.get("logo_img"));
        editor.putString("qrcode_file", hm.get("qrcode_file"));
        editor.putString("qrcode_file", hm.get("qrcode_file"));
        editor.putString("summary", hm.get("summary"));
        editor.putString("levl", hm.get("levl"));
        editor.putString("type", hm.get("type"));

        // 提交修改
        editor.commit();
    }
}
